﻿// -----------------------------------------------------------------------
// <copyright file="DmDichVuChiTietDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmDichVuChiTietDataAccess
    {
        public bool InsertDmDichVuChiTiet(DmDichVuChiTietEntity obj)
        {
            try
            {
                string sql = "spDmDichVuChiTiet_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "TenChiTieu", DbType.String, obj.TenChiTieu);
                db.AddInParameter(cmd, "TriSoBinhThuong", DbType.String, obj.TriSoBinhThuong);
                db.AddInParameter(cmd, "DonVi", DbType.String, obj.DonVi);
                db.AddInParameter(cmd, "SoThuTu", DbType.Int32, obj.SoThuTu);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, obj.MaDichVu);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmDichVuChiTiet", ex.Message); throw ex; }
        }

        public bool EditDmDichVuChiTiet(DmDichVuChiTietEntity obj)
        {
            try
            {
                string sql = "spDmDichVuChiTiet_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "TenChiTieu", DbType.String, obj.TenChiTieu);
                db.AddInParameter(cmd, "TriSoBinhThuong", DbType.String, obj.TriSoBinhThuong);
                db.AddInParameter(cmd, "DonVi", DbType.String, obj.DonVi);
                db.AddInParameter(cmd, "SoThuTu", DbType.Int32, obj.SoThuTu);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, obj.MaDichVu);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmDichVuChiTiet", ex.Message); throw ex; }
        }

        public bool DeleteDmDichVuChiTiet(string maChiTieu)
        {
            try
            {
                string sql = "spDmDichVuChiTiet_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, maChiTieu);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmDichVuChiTiet", ex.Message); throw ex; }
        }

        public DmDichVuChiTietEntity GetDmDichVuChiTiet(string maChiTieu)
        {
            try
            {
                DmDichVuChiTietEntity obj = null;
                string sql = "spDmDichVuChiTiet_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, maChiTieu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmDichVuChiTietEntity();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.TenChiTieu = reader["TenChiTieu"].ToString();
                        obj.TriSoBinhThuong = reader["TriSoBinhThuong"].ToString();
                        obj.DonVi = reader["DonVi"].ToString();
                        obj.SoThuTu = String.IsNullOrEmpty(reader["SoThuTu"].ToString()) == true ? 0 : (Int32)reader["SoThuTu"];
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmDichVuChiTiet", ex.Message); throw ex; }
        }

        public List<DmDichVuChiTietEntity> GetDmDichVuChiTietByDichVu(string maDichVu)
        {
            try
            {
                List<DmDichVuChiTietEntity> list = new List<DmDichVuChiTietEntity>();
                DmDichVuChiTietEntity obj = null;
                string sql = "spDmDichVuChiTiet_SelectByMaDichVu";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, maDichVu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmDichVuChiTietEntity();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.TenChiTieu = reader["TenChiTieu"].ToString();
                        obj.TriSoBinhThuong = reader["TriSoBinhThuong"].ToString();
                        obj.DonVi = reader["DonVi"].ToString();
                        obj.SoThuTu = String.IsNullOrEmpty(reader["SoThuTu"].ToString()) == true ? 0 : (Int32)reader["SoThuTu"];
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.TenDichVu = reader["TenDichVu"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
               
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmDichVuChiTietByDichVu", ex.Message); throw ex; }
        }

        public List<DmDichVuChiTietEntity> GetDmDichVuChiTietList()
        {
            try
            {
                List<DmDichVuChiTietEntity> list = new List<DmDichVuChiTietEntity>();
                DmDichVuChiTietEntity obj = null;
                string sql = "spDmDichVuChiTiet_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmDichVuChiTietEntity();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.TenChiTieu = reader["TenChiTieu"].ToString();
                        obj.TriSoBinhThuong = reader["TriSoBinhThuong"].ToString();
                        obj.DonVi = reader["DonVi"].ToString();
                        obj.SoThuTu = String.IsNullOrEmpty(reader["SoThuTu"].ToString()) == true ? 0 : (Int32)reader["SoThuTu"];
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.TenDichVu = reader["TenDichVu"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmDichVuChiTietList", ex.Message); throw ex; }
        }

    }
}